
/*

Design Pattern Automation Toolkit.
Application to create applications with emphasis on Design patterns.
And support for round trip engineering.
Copyright (C) 2004 Vineeth Neelakant. nvineeth@gmail.com

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/
using System;using BusinessLayer;
using InfoClasses;
using dpatoolkit.UILayer;
using System.Drawing;
using System.Collections;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Windows.Forms;
using DesignPatternDefinitions;
using WeifenLuo.WinFormsUI.Docking;

namespace dpatoolkit.UILayer
{
	
	/// <summary>
	/// Summary description for ComponentForm.
	/// </summary>
	
	public class ComponentForm : DockContent /*System.Windows.Forms.Form */, IMdiChild 
	{
		private System.ComponentModel.IContainer components;
		private ClassDiagram uicomponent;
		private IDesignPatternSubject subject;
		private MenuItem addDPMenuItem;
        private IUIProjectUnit project;

		#region Constructor and Dispose
		public ComponentForm(string CompName , IDesignPatternSubject sub)
		{
			if(sub==null)
				throw new ArgumentNullException("The subject cannot be null!");
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			subject=sub;
			this.WindowState = FormWindowState.Maximized;
			this.Text=CompName;
			this.uicomponent = new ClassDiagram(CompName , sub, this);
			this.Controls.Add(uicomponent);
            this.uicomponent.AdjustLocation();
            this.uicomponent.SizeChanged += new EventHandler(uicomponent_SizeChanged);
            this.InitDPMenu();

		}

        /// <summary>
        /// Event handler for Class Diagram size changed event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void uicomponent_SizeChanged(object sender, EventArgs e)
        {
            int width = this.Size.Width, height = this.Size.Height;
            if (this.Size.Width < this.uicomponent.Size.Width)
                width = this.uicomponent.Size.Width + 5;

            if (this.Size.Height < this.uicomponent.Size.Height)
                height = this.uicomponent.Size.Height + 5;
            
            this.Size = new Size(width, height);
        }

		private void InitDPMenu()
		{
			try
			{
				this.addDPMenuItem = new MenuItem("Add a DesignPattern to this component");
				this.uicomponent.ContextMenu2.MenuItems.Add(this.addDPMenuItem);
				//this.addDPMenuItem.Click+=new EventHandler(addDPMenuItem_Click);
				MenuItem[] items = dpatoolkit.GlobalFactory.CreateDesignPatternMenuItems();
				this.addDPMenuItem.MenuItems.AddRange(items);
				foreach ( MenuItem _item in items )
				{
					foreach(MenuItem dpMenuItem in _item.MenuItems)
					{
						dpMenuItem.Click+=new EventHandler(dpMenuItem_Click);
					}
				}
                // Add from file menu : Temporary Hack.
                MenuItem addFileMenuItem = new MenuItem("Add From File");
                addFileMenuItem.Click += new EventHandler(AddDPFromFile_Click);
                this.addDPMenuItem.MenuItems.Add(addFileMenuItem);
			}
			catch(Exception e)
			{
				ErrorMessage.Show("Error while constructing the design pattern menu! Details:"+e.ToString()); 
			}
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}
		#endregion

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() 
		{
            this.SuspendLayout();
            // 
            // ComponentForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.AutoScroll = true;
            this.BackColor = System.Drawing.SystemColors.ControlLight;
            this.ClientSize = new System.Drawing.Size(696, 352);
            this.HideOnClose = true;
            this.Name = "ComponentForm";
            this.TabText = "Component Class Diagram";
            this.Text = "Component Class Diagram";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.ResumeLayout(false);

		}
		#endregion


		#region IMdiChild Members

        public void Print(Graphics g)
        {
            this.uicomponent.Print(g);
        }

		public void Print()
		{
            this.uicomponent.Print();
		}

		public void ClearAll()
		{
			this.uicomponent.ClearAll();
		}

		public void SaveAsImage(string path)
		{
			this.uicomponent.SaveAsImage(path);
		}

		public void MergePattern(string pathToPatternFile)
		{
			//During the merge of a Design pattern, the 
			//UIComponent design is completely cleared 
			//and redrawn. This is more clear and efficient.
			
			//tell the Subject to merge a pattern and the UIComponent to update after the merge.
			this.subject.Merge( this.Text,pathToPatternFile);
			
		}

		public string Title
		{
			set
            { 
                this.TabText = (string)value;
                this.uicomponent.Text = value;
            }
			get{ return this.TabText; }
		}

		public new void Close()
		{
			base.Close();
		}

        public IUIProjectUnit Project
        {
            get { return this.project; }
            set { this.project = value; }
        }
		
		#endregion

		private void addDPMenuItem_Click(object sender, EventArgs e)
		{
			this.MergePattern(string.Empty);
		}

		private void dpMenuItem_Click(object sender, EventArgs e)
		{
			MenuItem item = sender as MenuItem;
			if(item == null)
				return ;
			//find the parent menuitem.
			MenuItem parentMenu = item.Parent as MenuItem;
			if(parentMenu == null)
				return;
			string pathToDP = dpatoolkit.GlobalFactory.ConstructPathToPattern(parentMenu.Text,item.Text);
			this.MergePattern(pathToDP);
		}

		private void AddDPFromFile_Click(object sender, EventArgs e)
		{
			this.MergePattern(string.Empty);
		}
	}
}

